<?php

namespace app\api\controller;

use app\api\model\UserTwo as UserModel;
use app\api\model\Merchant as MerchantModel;
use think\facade\Session;

class User extends Controller
{
	/**
     *手机号登录
     */
	public function addLogin()
    {
        $where = $this->request->post();
        if(empty($where['username'])||empty($where['password'])){
            return $this->renderError(1001,'缺少必须参数',$data=[]);
        }
        $model = new UserModel;
		//$user=$model->app_login($this->request->post());
        $user=$model->addLogin($where);
		if($user){
            $statalist = $model->leixinglist($user);
			return $this->renderSuccess(1000,'登录成功',[
				'user_id' => $user,
				'token' => $model->getToken(),
                'state' => $statalist
			]);
		}
		return $this->renderError(1002,'登录失败');
    }

    /*
	*手机号拉取验证码
	* @return array
	*/
    public function addYanzm(){
        $model = new UserModel;
        $info = $this->request->post();

        if(empty($info['phone'])){
            return $this->renderError('1004','手机号不能为空',$data = []);
        }
        if(empty($info['type'])){
            $user = $model::useGlobalScope(false)->where([
                'phone' => $info['phone']
            ])->find();
            if($user){
                return $this->renderError('1001','该手机号已注册过',$data = []);//该手机号已注册过
            }
        }

        $code = rand('1000','9999');
        //$code = '1234';
        $this->duanxin($info['phone'],$code);
        $where = ['phone'=>$info['phone'],'code'=>$code,'create_time'=>time()];
        $user=$model->addYanzm($where,$info['phone']);

        if($user == '1002'){
            return $this->renderError('1002','记录失败',$data = []);
        }elseif($user == '1003'){
            return $this->renderError('1003','验证码15分钟内有效',$data = []);
        }else{
            return $this->renderSuccess('1000','发送成功',$data = []);
        }
    }

    /*
	*手机号注册
	* @return array
	*/
    public function addPhone(){
        $model = new UserModel;
        $info = $this->request->post();
        if(empty($info['phone'])||empty($info['code'])||empty($info['password'])){
            return $this->renderError('1005','必须参数不能为空',$data = []);
        }
        $user=$model->addPhone($info);
        if($user == '1001'){
            return $this->renderError('1001','该手机号已注册过',$data = []);
        }elseif($user == '1003'){
            return $this->renderError('1003','请获取发送验证码',$data = []);
        }elseif($user == '1002'){
            return $this->renderError('1002','发生未知错误',$data = []);
        }else{
            return $this->renderSuccess('1000','注册成功',[
                'user_id' => $user,
                'token' => $model->getToken()
            ]);

        }

    }

    /*
	*密码修改
	* @return array
	*/
    public function mimaUpdate(){
        $model = new UserModel;
        $info = $this->request->post();
        if(empty($info['phone'])||empty($info['code'])||empty($info['password'])){
            return $this->renderError('1005','必须参数不能为空',$data = []);
        }
        $user=$model->mimaUpdate($info);
        if($user == '1001'){
            return $this->renderError('1001','该手机号未注册过',$data = []);
        }elseif($user == '1003'){
            return $this->renderError('1003','请获取发送验证码',$data = []);
        }elseif($user == '1002'){
            return $this->renderError('1002','发生未知错误',$data = []);
        }else{
            Session::clear();
            return $this->renderSuccess('1000','修改成功');

        }

    }

    /**
     *退出登录
     */
    public function logOut(){
        Session::clear();
        return $this->renderSuccess('1000','成功');
    }

    public  function duanxin($phone,$code)
    {
        //$url = 'http://v.juhe.cn/sms/send';//post查询条件
        //$fields = 'postid=75159251697701&id=1&valicode=&temp=0.006414744938973982&type=zhongtong&phone=&token=&platform=MWWW';
        //$fields = 'mobile=18510852363&tpl_id=176606&tpl_value=%23code%23%3D654654&key=9cace9e00efd9d51f12f2c255e4a40be';
        //$url = 'http://106.14.255.200:8888/sms.aspx';
        //$fields = 'action=send&userid=12&account=艺人装修&password=密码&mobile=18510852363&content=12345678912&sendTime=&extno=';
        $url = 'http://utf8.api.smschinese.cn/';
        $fields = 'Uid=艺人装修平台&Key=d41d8cd98f00b204e980&smsMob='.$phone.'&smsText=【艺人装修】您的验证码是'.$code.',请妥善保管。';
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_HTTPHEADER => array(
                "accept: */*",
                "accept-encoding: gzip, deflate",
                "accept-language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,ja;q=0.2",
                "cache-control: no-cache",
                "connection: keep-alive",
                "content-type: application/x-www-form-urlencoded",
                //"cookie: $cookie",
                "origin: http://www.**.com",
                "pragma: no-cache",
                "referer: http://www.****.com",
                "user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
                "x-requested-with: XMLHttpRequest"
            ),
        ));

//url
        curl_setopt($curl, CURLOPT_URL, $url);
//post fields
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($curl, CURLOPT_COOKIE, 'csrftoken=mRRKbKMm34ocwmyBGNSnLp4Tk0zLILC9Tihjg0P2dX0; Hm_lvt_22ea01af58ba2be0fec7c11b25e88e6c=1564563968; WWWID=WWW31AAF56D6B1EBE11C65DA00F8BC534D9; Hm_lpvt_22ea01af58ba2be0fec7c11b25e88e6c=1564566015');
        $response = curl_exec($curl);
        $err = curl_error($curl);
        $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        return $response;
    }


}
